import history from '@/utils/history'
import { hasToken } from '@/utils/storage'
import { FC } from 'react'
import { Redirect, Route } from 'react-router'
type props = {
  component: FC
  path: string
}
export default function Auth({ path, component: Component }: props) {
  return (
    <Route
      path={path}
      render={() => {
        if (hasToken()) {
          return <Component />
        } else {
          // 如果没有token，回到登录页并且记录跳转之前的页面
          return (
            <Redirect
              to={{
                pathname: '/login',
                state: { from: history.location.pathname },
              }}
            />
          )
        }
      }}
    ></Route>
  )
}
